package com.lwh.yygh.hosp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwh.yygh.hosp.mapper.HospitalMapper;
import com.lwh.yygh.hosp.repository.HospitalRepository;
import com.lwh.yygh.hosp.service.HospitalService;
import com.lwh.yygh.model.hosp.Hospital;
import com.lwh.yygh.model.hosp.HospitalSet;
import com.lwh.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

/**
 * @author lwh
 * @date 2021/5/16 15:18
 *      ServiceImpl参数：M extends BaseMapper<T>，一个mapper的实体类
 *                      T，我们自己的实体类
 */
@Service
public class HospitalServiceImpl extends ServiceImpl<HospitalMapper, HospitalSet> implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Override
    public String getSignKey(String hoscode) {
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hoscode",hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(queryWrapper);
        return hospitalSet.getSignKey();
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo queryVo) {
        //创建Pageable
        Pageable pageable = PageRequest.of(page-1,limit);
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        //将HospitalVo转为Hospital
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(queryVo,hospital);
        Example<Hospital> example = Example.of(hospital, matcher);
        //调用mongodb方法
        Page<Hospital> all = hospitalRepository.findAll(example, pageable);
        return all;
    }
}
